﻿using System;
using Level;
using UnityEngine;
using UnityEngine.UI;

namespace UI
{
    public class PauseMenuUi : MonoBehaviour
    {
        [SerializeField] private PauseState pauseState;
        
        [SerializeField] private SceneLoader sceneLoader;
        
        [SerializeField] private Button exitButton;
        [SerializeField] private Button closeMenuButton;
        [SerializeField] private Button restartButton;

        private UiFader _uiFader;


        private void Awake()
        {
            _uiFader = GetComponent<UiFader>();
            
            closeMenuButton.onClick.AddListener(CloseMenu);
            restartButton.onClick.AddListener(Restart);
            exitButton.onClick.AddListener(Exit);
        }

        private void OnEnable()
        {
            pauseState.Paused += OnPaused;
            pauseState.Resumed += OnResumed;
        }

        private void OnDisable()
        {
            pauseState.Paused -= OnPaused;
            pauseState.Resumed -= OnResumed;
        }

        private void OnResumed()
        {
            _uiFader.Hide();
        }

        private void OnPaused()
        {
            _uiFader.Show();
        }

        private void CloseMenu()
        {
            pauseState.Resume();
        }
        
        private void Restart()
        {
            sceneLoader.LoadScene(gameObject.scene.buildIndex);
        }

        private void Exit()
        {
            sceneLoader.LoadScene(0);
        }
    }
}